package operation;

import JDBC.JDBCUtils;
import Medicine.Pharmacy;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.Scanner;


public class ChangePriceOfEqu implements IOPeration{
    @Override
    public void work(Pharmacy pharmacy) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要更新价格的器械名称: ");
        String name = sc.nextLine();
        System.out.println("请输入新的器械价格: ");
        double newPrice = sc.nextDouble();
        updatePrice(name, newPrice);
    }
        public static void updatePrice(String name, double newPrice) throws SQLException {
            QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
            String sql = "UPDATE Equipment SET Price=? WHERE Name=?";
            Object[] params = {newPrice, name};
            int count = qr.update(sql, params);
            if (count < 1) {
                System.out.println("更新价格失败，没有找到对应的器械或更新不成功。");
            } else {
                System.out.println("更新价格成功！");
            }
        }

}


